package Utils.UI;

import java.util.List;

import main.Session;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import datatype.User;
import datatype.DBtypes.ProgramfromDB;
import datatype.searchtype.SearchResult;
import datatype.searchtype.SearchResult.MediaRes;
import datatype.searchtype.SearchResult.PersonRes;

/**
 * UIUpdater simulates the execution of a "UI thread" on a single display (of
 * the current widget) using the SWT asyncExec method. It consists of basic
 * operations on widgets we will use.
 * 
 * @author micha
 * 
 */

public class UIUpdater {

	public static void setWidgetText(final Widget widget, final String text) {

		widget.getDisplay().asyncExec(new Runnable() {
			public void run() {
				if (widget.isDisposed())
					return;

				if (widget instanceof Label) {
					((Label) widget).setText(text);
				}

				else if (widget instanceof Text) {
					((Text) widget).setText(text);
				}
			}
		});
	}

	public static void setWidgetVisibile(final Widget widget,
			final boolean visible) {

		widget.getDisplay().asyncExec(new Runnable() {
			public void run() {
				if (widget.isDisposed())
					return;

				if (widget instanceof Label) {
					((Label) widget).setVisible(visible);
				}

				else if (widget instanceof Text) {
					((Text) widget).setVisible(visible);
				}

				else if (widget instanceof Button) {
					((Button) widget).setVisible(visible);
				} else if (widget instanceof Canvas)
					((Canvas) widget).setVisible(visible);
			}
		});
	}

	public static void setWidgetEnable(final Widget widget, final boolean enable) {

		widget.getDisplay().asyncExec(new Runnable() {
			public void run() {
				if (widget.isDisposed())
					return;

				if (widget instanceof Label) {
					((Label) widget).setEnabled(enable);
				}

				else if (widget instanceof Text) {
					((Text) widget).setEnabled(enable);
				}

				else if (widget instanceof Button) {
					((Button) widget).setEnabled(enable);
				}

			}
		});
	}

	public static void timerTimeup(Display display) {
		display.asyncExec(new Runnable() {

			@Override
			public void run() {

				if (Session.game == null) {
					return;
				}

				Session.game.finishGame();

				Session.gui.showTextBoxWithOk(" time's up!!",
						"damm, time is gone!!", SWT.ICON_WORKING);

				Session.gui.getPlay().showStatsScreen(false);

			}
		});
	}

	public static void loginChangeGUI(Display display) {
		display.asyncExec(new Runnable() {

			@Override
			public void run() {

				Session.gui.introLoginBTN_afterDB();
				// changeScreenTo(mainScreen);
			}
		});
	}

	public static void registrationChangeGUI(Display display, final int userID) {
		display.asyncExec(new Runnable() {

			@Override
			public void run() {
				Session.gui.registration_afterDB(userID);
				// changeScreenTo(mainScreen);
			}
		});
	}

	public static void changeScreen(final Composite c) {

		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				Session.gui.changeScreenTo(c);
				System.out.println("sdsds");

			}
		});

	}

	public static void updateUsersTable(final List<User> users) {

		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				Session.gui.getUser_managment().setUsers(users);
				Session.gui.getUser_managment().createTableLayout();
				Session.gui.getUser_managment().prepareTheTable();
			}
		});

	}

	public static void updateSearchTBL(final SearchResult searchRes) {
		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				Session.gui.getSearchDB().setSearchResults(searchRes);
				Session.gui.getSearchDB().updateTables();
			}
		});

	}

	public static void clearAddPersonWindow() {

		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				Session.gui.getSearchDB().clearAddWindow();
			}
		});
	}

	public static void updatePersonTable(final List<PersonRes> personRes,
			final Table table, final Button add, final boolean isProgram,
			final boolean isActor) {
		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {

				if (isProgram) {
					if (isActor)
						Session.gui.getEditProgram().setActorRes(personRes);
					else
						Session.gui.getEditProgram().setCreatorRes(personRes);
				} else {
					if (isActor)
						Session.gui.getEditFilm().setActorRes(personRes);
					else
						Session.gui.getEditFilm().setDirectorRes(personRes);
				}

				table.removeAll();
				for (PersonRes pr : personRes) {
					TableItem ti = new TableItem(table, SWT.NONE);
					ti.setText(0, pr.getName());
					ti.setText(1, pr.getKnownFor());
				}

				add.setVisible(true);
			}
		});
	}

	public static void updateMediaTable(final List<MediaRes> mediaRes,
			final Table table, final Button add) {
		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {

				Session.gui.getEditFilm().setFilmsRes(mediaRes);

				table.removeAll();
				for (MediaRes mr : mediaRes) {
					TableItem ti = new TableItem(table, SWT.NONE);
					ti.setText(0, mr.getName());
					ti.setText(1, "" + mr.getReleaseYear());
				}
				if (!mediaRes.isEmpty())
					add.setVisible(true);
			}
		});
	}

	public static void prepareMediaPage(final Composite c,
			final boolean isProgram) {
		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				if (isProgram)
					Session.gui.getEditProgram().prepareProgramPage();
				else
					Session.gui.getEditFilm().prepareFilmPage();
				Session.gui.changeScreenTo(c);
			}
		});

	}

	public static void showErrMsg(final String errMsg) {
		Session.gui.getDisplay().asyncExec(new Runnable() {
			@Override
			public void run() {
				Session.gui.showErrorMsgBox(errMsg);

			}
		});
	}

	public static void updateLoader(final int imageNum, final Canvas canvas,
			final GC gc, final ImageLoader loader) {
		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				if (!canvas.isDisposed() && !canvas.isVisible())
					canvas.setVisible(true);
				
		/*		ImageData trans = new ImageData(canvas.getSize().x, canvas
						.getSize().y, 1, new PaletteData(
						new RGB[] { (Session.gui.getDisplay().getSystemColor(SWT.COLOR_WHITE)).getRGB(),
								(Session.gui.getDisplay().getSystemColor(SWT.COLOR_BLACK)).getRGB() }));
				trans.transparentPixel = 0;
				Image ing = new Image(Session.gui.getDisplay(), trans);
				gc.drawImage(ing, trans.x, trans.y);
				ing.dispose();
			*/	
				ImageData nextImg = loader.data[imageNum];
				Image img = new Image(Session.gui.getDisplay(), nextImg);
				gc.drawImage(img, nextImg.x, nextImg.y);
				img.dispose();
				if (!canvas.isDisposed()) {
					canvas.redraw();
				}
			}
		});

	}

	public static void changePsssGUI() {
		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				Session.gui.afterChangePass();

			}
		});

	}

	public static void editDB(final String info) {
		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				Session.gui.editDB_afterDB(info);

			}
		});
		
	}

	public static void afterConnecting() {
		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				Session.gui.afterConnecting();

			}
		});
	}

	public static void afterDBmanage() {
		Session.gui.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				Session.gui.afterDBmanage();

			}
		});
	}
}
